﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.BO;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using Guru.TransactionLogger;
using Guru.TransactionLogger.Source.BO;
using System;
using System.Data;
using System.Resources;

namespace CRMG.Container.Views.Lookup
{
    public class FrmVendor : FrmLookupBase
    {
        int _vendorId;
        DataTable dtVendors;
        LookupManager lookupMan;

        public FrmVendor()
            : this(-1)
        {
        }

        public FrmVendor(int vendorId)
            : base()
        {
            try
            {
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmVendor).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmVendor).Assembly);
                _vendorId = vendorId;
                lookupMan = new LookupManager();
                dtVendors = lookupMan.GetVendors();
                this.Text = formRes.GetString("FrmVendor.Caption");
                this.lblName.Text = formRes.GetString("FrmVendor.lblVendor");
                this.btnSave.Text = vendorId != -1 ? formRes.GetString("Update") : formRes.GetString("Add");
                this.btnCancel.Text = formRes.GetString("Cancel");

                if (vendorId != -1)
                {
                    this.txtName.Text = DataUtil.GetObjectWithSelectedColumn(dtVendors, "VendorName", vendorId, "OBJID").ToStr();
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmVendor.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2090,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2090)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmVendor.Ctor.Error"),
                      messageRes.GetString("Error"));
            }
            finally
            {
                btnSave.Enabled = false;
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            try
            {
                int vndrId = DataUtil.GetObjectWithSelectedColumn(dtVendors, "VendorName", txtName.Text, "OBJID").ToInt();
                if (vndrId > 0 && vndrId != _vendorId)
                {
                    ControlUtil.ShowWarningMessage(
                    messageRes.GetString("FrmVendor.Save.CompanyAlreadyExist"),
                    messageRes.GetString("Warning"));
                    return;
                }
                else
                {
                    Vendor vendor = new Vendor() { VendorName = txtName.Text };
                    int Id = -1;

                    if (_vendorId == -1)
                    {
                        Id = lookupMan.AddVendor(vendor);
                    }
                    else
                    {
                        vendor.OBJID = _vendorId;
                        lookupMan.UpdateVendor(vendor);
                    }

                    TransactionLogEntry logEntry = new TransactionLogEntry(AppVariables.CurrentUser.OBJID)
                    {
                        FormName = this.Name,
                        TransactionType = _vendorId == -1 ? TransactionTypes.Insert : TransactionTypes.Update,
                        ControlName = btnSave.Text,
                        ControlTag = btnSave.Tag.ToStr(),
                        LogObject = _vendorId == -1 ? Id : _vendorId
                    };
                    logEntry.Insert();

                    btnSave.Enabled = false;
                    btnCancel.Text = formRes.GetString("Close");
                    ControlUtil.ShowInfoMessage(
                      messageRes.GetString("FrmVendor.Save.Success"),
                      messageRes.GetString("Info"));
                }
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmVendor.Save.Error"),
                      messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2091,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2091)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                      messageRes.GetString("FrmVendor.Save.Error"),
                      messageRes.GetString("Error"));
            }
        }

    }
}
